//
//  WKPageCell.m
//  CosChat
//
//  Created by Mark on 15/4/27.
//  Copyright (c) 2015年 yq. All rights reserved.
//

#import "WKPageCell.h"
#import "BCStuffSecondTableCell.h"
#import "BCStuffThirdTableCell.h"
@interface WKPageCell ()<UITableViewDelegate,UITableViewDataSource,actionDelegate>
@property (nonatomic,assign)NSInteger chooseNum;
@property (nonatomic,strong) CALayer *dotLayer;
@property (nonatomic,assign) CGFloat endPointX;
@property (nonatomic,assign) CGFloat endPointY;
@property (nonatomic,strong) UIBezierPath *path;
@end

@implementation WKPageCell
- (id)initWithIdentifier:(NSString *)identifier{
    if (self = [super init]) {
        _identifier = identifier;

        [self createView];
    }
    return self;
}
- (void)createView{
    self.chooseNumArr = [NSMutableArray array];

    self.secondListTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 80*wx, SCREEN_HEIGHT-180 *hx) style:UITableViewStylePlain];
    self.secondListTable.backgroundColor = [UIColor colorWithRed:0.949  green:0.949  blue:0.949 alpha:1];
    self.thirdListTable = [[UITableView alloc] initWithFrame:CGRectMake(80 * wx, 0, SCREEN_WIDTH - 80 *wx, SCREEN_HEIGHT-65.f-100.f) style:UITableViewStylePlain];
    self.secondListTable.delegate = self;
    self.secondListTable.dataSource = self;
    [self.secondListTable registerClass:[BCStuffSecondTableCell class] forCellReuseIdentifier:@"secondReuse"];
    self.secondListTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.secondListTable.backgroundColor = [UIColor colorWithRed:0.972  green:0.973  blue:0.972 alpha:1];
    self.secondListTable.showsVerticalScrollIndicator = NO;
    
    UILabel *headLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 80 *wx, 27)];
    headLab.backgroundColor = [UIColor colorWithRed:0.949  green:0.949  blue:0.949 alpha:1];
    headLab.text = @" 标价 : 为邦采推荐价格.";
    headLab.font = [UIFont systemFontOfSize:12];
    headLab.textColor = [UIColor colorWithRed:0.044  green:0.513  blue:0.987 alpha:1];
    
    self.thirdListTable.tableHeaderView = headLab;
    
    self.thirdListTable.delegate = self;
    self.thirdListTable.dataSource = self;
    self.thirdListTable.backgroundColor = [UIColor colorWithRed:0.972  green:0.973  blue:0.972 alpha:1];
    [self.thirdListTable registerClass:[BCStuffThirdTableCell class] forCellReuseIdentifier:@"thirdReuse"];
    [self addSubview:self.secondListTable];
    [self addSubview:self.thirdListTable];
    self.backgroundColor = [UIColor colorWithRed:0.949  green:0.949  blue:0.949 alpha:1];
    _chooseNum = 0;
    
    //分类小红点
    _endPointX = SCREEN_WIDTH*0.02f;
    _endPointY = SCREEN_HEIGHT*0.95f;
}

- (void)setSecondArr:(NSMutableArray *)secondArr
{
    _secondArr = secondArr;
    [self.secondListTable reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == self.secondListTable) {
        return self.secondArr.count;
    }else
    {
        return 10;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.secondListTable) {
        return 60;
    }else
    {
        return 118;
    }
}

- (void)pushWithData:(NSDictionary *)datadic
{
    [self.thirdListTable reloadData];

}

- (void)setChooseNumArr:(NSMutableArray *)chooseNumArr
{
    _chooseNumArr = chooseNumArr;
    [self.thirdListTable reloadData];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (tableView == self.secondListTable) {
        ;
    }else{
    
    BCStuffThirdTableCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.plus.selected = !cell.plus.selected;

    if (tableView == self.secondListTable) {
        _chooseNum = indexPath.row;
        [self.delegate goSecond];
        [self.secondListTable reloadData];
    }else
    {
        NSMutableArray *arr = [NSMutableArray arrayWithArray:self.chooseNumArr];
        BOOL isin = NO;
        if (self.chooseNumArr.count == 0) {
            [arr addObject:[NSNumber numberWithInteger:indexPath.row]];
            [self.delegate addAmount];

                }else{
        for (NSNumber *num in self.chooseNumArr) {
                if (num.integerValue == indexPath.row) {
                    [arr removeObject:[NSNumber numberWithInteger:indexPath.row]];
                    isin = YES;
                    [self.delegate removeAmount];
                }
            }
            if (!isin) {
                [arr addObject:[NSNumber numberWithInteger:indexPath.row]];
                [self.delegate addAmount];
            }
        }
        self.chooseNumArr = arr;
        [self.delegate getAmount:arr index:self.index];
    }

    }
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.secondListTable) {
        BCStuffSecondTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"secondReuse"];
        cell.title = self.secondArr[indexPath.row ];
        if (indexPath.row == _chooseNum) {
            cell.choosed = YES;
        }else{
            cell.choosed = NO;
        }
        return cell;

    }else   {
        BCStuffThirdTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"thirdReuse"];
        cell.name = @"培根肉卷10斤/箱";
        cell.price = 145;
        
       
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.name = [NSString stringWithFormat:@"猪肉培根肉卷%ld斤一箱", (long)indexPath.row];
        cell.delegate = self;
        cell.plus.selected = NO;
        for (NSNumber *num in self.chooseNumArr) {
            if (indexPath.row == num.integerValue) {
                cell.plus.selected = YES;
            }
        
        }
        return cell;

      
    }

}

- (void)reloadData
{
    [self.thirdListTable reloadData];
}

#pragma mark -加入购物车动画
-(void) JoinCartAnimationWithRect:(CGRect)rect
{
    
    CGFloat startX = rect.origin.x;
    CGFloat startY = rect.origin.y;
    
    _path= [UIBezierPath bezierPath];
    [_path moveToPoint:CGPointMake(startX, startY)];
    //三点曲线
    [_path addCurveToPoint:CGPointMake(_endPointX, _endPointY)
             controlPoint1:CGPointMake(startX, startY)
             controlPoint2:CGPointMake(startX - 180, startY - 200)];
    
    _dotLayer = [CALayer layer];
    _dotLayer.backgroundColor = [UIColor redColor].CGColor;
    _dotLayer.frame = CGRectMake(0, 0, 15, 15);
    _dotLayer.cornerRadius = (15 + 15) /4;
    [self.layer addSublayer:_dotLayer];
    [self groupAnimation];
    
}
#pragma mark - 组合动画
-(void)groupAnimation
{
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    animation.path = _path.CGPath;
    animation.rotationMode = kCAAnimationRotateAuto;
    
    CABasicAnimation *alphaAnimation = [CABasicAnimation animationWithKeyPath:@"alpha"];
    alphaAnimation.duration = RedAnimationDuring;
    alphaAnimation.fromValue = [NSNumber numberWithFloat:1.0];
    alphaAnimation.toValue = [NSNumber numberWithFloat:0.1];
    alphaAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    
    CAAnimationGroup *groups = [CAAnimationGroup animation];
    groups.animations = @[animation,alphaAnimation];
    groups.duration = RedAnimationDuring+0.3f;
    groups.removedOnCompletion = NO;
    groups.fillMode = kCAFillModeForwards;
    groups.delegate = self;
    [groups setValue:@"groupsAnimation" forKey:@"animationName"];
    [_dotLayer addAnimation:groups forKey:nil];
    
    [self performSelector:@selector(removeFromLayer:) withObject:_dotLayer afterDelay:RedAnimationDuring+0.3f];
    
}
- (void)removeFromLayer:(CALayer *)layerAnimation{
    
    [layerAnimation removeFromSuperlayer];
}

#pragma mark - CAAnimationDelegate
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    _reddotAnimationBlock(anim);
}

@end
